﻿using System.Web.Security;
using AspAuthentication.Core;
using AspAuthentication.Exceptions;
using AspAuthentication.Interfaces;
using AspAuthentication.Providers;

namespace AspAuthentication
{
    /// <summary>
    /// Defines the class UserFormsAuthentication
    /// </summary>
    public class UserFormsAuthentication : IUserFormsAuthentication
    {
        private IPortalMembershipProvider _membershipProvider;
        private IFormsAuthenticationProvider _formsAuthenticationProvider;

        /// <summary>
        /// Gets or sets the forms authentication provider.
        /// </summary>
        /// <value>The forms authentication provider.</value>
        public IFormsAuthenticationProvider FormsAuthenticationProvider
        {
            get {
                return _formsAuthenticationProvider ??
                       (_formsAuthenticationProvider = new FormsAuthenticationProvider());
            }
            set { _formsAuthenticationProvider = value; }
        }

        /// <summary>
        /// Gets or sets the membership provider.
        /// </summary>
        /// <value>The membership provider.</value>
        public IPortalMembershipProvider MembershipProvider
        {
            get { return _membershipProvider ?? (_membershipProvider = (PortalMembershipProvider)Membership.Provider); }
            set { _membershipProvider = value; }
        }

        /// <summary>
        /// Logins the specified user name.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="password">The password.</param>
        /// <param name="createPersistentCookie">if set to <c>true</c> [create persistent cookie].</param>
        public void Login(string userName,string password, bool createPersistentCookie)
        {
            if (MembershipProvider.LoginUser(userName,password))
            {
                FormsAuthenticationProvider.SetAuthCookie(userName, createPersistentCookie);
            }
            else
            {
                throw new AspAuthenticationException("Could not authenticate the user with the supplied credentials!");
            }
        }

        /// <summary>
        /// Logins the specified login container.
        /// </summary>
        /// <param name="loginContainer">The login container.</param>
        public void Login(LoginContainer loginContainer)
        {
            if (MembershipProvider.LoginUser(loginContainer.Username,loginContainer.Password))
            {
                FormsAuthenticationProvider.SetAuthCookie(loginContainer.Username, loginContainer.RememberMe);
            }
            else
            {
                throw new AspAuthenticationException("Could not authenticate the user with the supplied credentials!");
            }
        }

        /// <summary>
        /// Logouts this instance.
        /// </summary>
        public void Logout()
        {
            try
            {
                MembershipProvider.LogoutUser();
            }
            catch
            {
                //Suppress exceptions for situations where the user is not really logged in.
                //Especially when the user has logged out in another browser tab.
            }

            FormsAuthenticationProvider.SignOut();
        }
    }
}